package com.gugee.tiktok.web.spider;

import com.gugee.tiktok.web.model.mysql.Blogger;
import com.gugee.tiktok.web.spider.model.User;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Mapper(componentModel = "spring")
public interface BloggerMapper {

    @Mapping(target = "oAvatar300", expression = "java(getAvatar_300x300(dto))")
    @Mapping(target = "oUid", source = "uid")
    @Mapping(target = "oAvatarMedium", expression = "java(getAvatar_medium(dto))")
    @Mapping(target = "oFollowerCount", source = "follower_count")
    @Mapping(target = "oDistrict", source = "district")
    @Mapping(target = "oSignature", source = "signature")
    @Mapping(target = "oMplatformFollowersCount", source = "mplatform_followers_count")
    @Mapping(target = "oCoverUrl", expression = "java(getCover_url(dto))")
    @Mapping(target = "oIsStar", source = "is_star")
    @Mapping(target = "oFollowingCount", source = "following_count")
    @Mapping(target = "oTotalFavorited", source = "total_favorited")
    @Mapping(target = "oDongtaiCount", source = "dongtai_count")
    @Mapping(target = "oAvatar168", expression = "java(getAvatar_168x168(dto))")
    @Mapping(target = "oGender", source = "gender")
    @Mapping(target = "oShareInfo", expression = "java(getShare_info(dto))")
    @Mapping(target = "oAvatarLarger", expression = "java(getAvatar_larger(dto))")
    @Mapping(target = "oBirthday", source = "birthday")
    @Mapping(target = "oCountry", source = "country")
    @Mapping(target = "oCity", source = "city")
    @Mapping(target = "oIsActivityUser", source = "is_activity_user")
    @Mapping(target = "oAvatarThumb", expression = "java(getAvatar_thumb(dto))")
    @Mapping(target = "oProvince", source = "province")
    @Mapping(target = "oAwemeCount", source = "aweme_count")
    @Mapping(target = "oLocation", source = "location")
    @Mapping(target = "oSecUid", source = "sec_uid")
    @Mapping(target = "oNickname", source = "nickname")
    @Mapping(target = "oRoomId", source = "room_id")
    @Mapping(target = "oRegion", source = "region")
    @Mapping(target = "oVideoIcon", expression = "java(getVideo_icon(dto))")
    @Mapping(target = "oUniqueId", source = "unique_id")
    @Mapping(target = "oConstellation", source = "constellation")
    @Mapping(target = "oEnterpriseVerifyReason", source = "enterprise_verify_reason")
    @Mapping(target = "oWithFusionShopEntry", source = "with_fusion_shop_entry")
    Blogger mapper(User dto);

    List<Blogger> mapper(List<User> dtos);

    default String getVideo_icon(User dto) {
        if (dto.getVideo_icon() == null || CollectionUtils.isEmpty(dto.getVideo_icon().getUrl_list())) {
            return "";
        }
        return dto.getVideo_icon().getUrl_list().get(0);
    }

    default String getCover_url(User dto) {
        if (CollectionUtils.isEmpty(dto.getCover_url())) return "";
        return dto.getCover_url().get(0).getUrl_list().get(0);
    }

    default String getShare_info(User dto) {
        return dto.getShare_info() == null ? "" : dto.getShare_info().getShare_url();
    }

    default String getAvatar_thumb(User dto) {
        if (CollectionUtils.isEmpty(dto.getAvatar_thumb().getUrl_list())) return "";
        return dto.getAvatar_thumb().getUrl_list().get(0);
    }

    default String getAvatar_larger(User dto){
        if (CollectionUtils.isEmpty(dto.getAvatar_larger().getUrl_list())) return "";
        return dto.getAvatar_larger().getUrl_list().get(0);
    }

    default String getAvatar_168x168(User dto){
        if (CollectionUtils.isEmpty(dto.getAvatar_168x168().getUrl_list())) return "";
        return dto.getAvatar_168x168().getUrl_list().get(0);
    }

    default String getAvatar_medium(User dto){
        if (CollectionUtils.isEmpty(dto.getAvatar_medium().getUrl_list())) return "";
        return dto.getAvatar_medium().getUrl_list().get(0);
    }

    default String getAvatar_300x300(User dto){
        if (CollectionUtils.isEmpty(dto.getAvatar_300x300().getUrl_list())) return "";
        return dto.getAvatar_300x300().getUrl_list().get(0);
    }
}
